package com.hexus.eve.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import com.hexus.eve.entities.Group;
import com.hexus.eve.utils.TranslationUtils;

public class Groups extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	private List<Group> groups= new ArrayList<Group>();
	private Integer categoryId;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public String execute() {
		if(categoryId != null) {
			Session session = sessionFactory.openSession();
			try {			
				groups = session.createQuery("from Group where published=1 and category=:categoryId order by name").setInteger("categoryId", categoryId).list();
				TranslationUtils.getInstance().translate(groups, getLocale());
				Collections.sort(groups);
			}
			catch(Exception ex) {
				Logger logger = Logger.getLogger(this.getClass().getName());
				logger.error(ex.getMessage());
				ex.printStackTrace();
			}
			finally {
				session.close();
			}
		}
		return SUCCESS;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public List<Group> getGroups() {
		return groups;
	}

}
